package com.situ.crm.controller.back;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Role;
import com.situ.crm.service.IRoleService;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private IRoleService roleService;

	// 跳转到展示界面
	@RequestMapping("/getrole")
	public String getrole() {

		return "/role/role";
	}

	// 分页查询
	@RequestMapping("/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer page, Integer limit) {
		ServerResponse serverResponse = roleService.pageList(page, limit);

		return serverResponse;
	}
	//查询role全部
	@RequestMapping("/selectAllRoles")
	@ResponseBody
	public ServerResponse selectAllRoles() {
		return roleService.selectAllRoles();
	}
	//跳转到添加界面
	@RequestMapping("/add")
	public String add() {
		
		return "/role/role_add";
	}
	//添加功能
	@RequestMapping("/insert")
	@ResponseBody
	public ServerResponse insert(Role role,String permisssions) {
		System.out.println(role);
		System.out.println(permisssions);
		return roleService.insert(role,permisssions);
	}
	//跳转到修改界面
	//根据携带的ID查询出role 的全部信息
	@RequestMapping("/toupdate")
	public String toupdate(Integer id,Model model) {
		System.out.println("roleId = "+id);
		Role role = roleService.selectById(id);
		model.addAttribute("role", role);
		
		return "/role/role_update";
	}
	//角色修改界面的权限展示
	@RequestMapping("/selectRoleAndPermissions")
	@ResponseBody
	public ServerResponse selectRoleAndPermissions(Integer roleId) {
		return roleService.selectRoleAndPermissions(roleId);
	}
	//修改功能
	@RequestMapping("/update")
	@ResponseBody
	public ServerResponse update(Role role,String permissions) {
		System.out.println(role);
		System.out.println(permissions);
		ServerResponse serverResponse = roleService.update(role,permissions);
		
		return serverResponse;
	}	
	
}
